//
//  LHCarouselView.m
//  轮播图
//
//  Created by zhouLiHuan on 2017/2/13.
//  Copyright © 2017年 zhouLiHuan. All rights reserved.
//

#import "LHCarouselView.h"

@implementation LHCarouselView


- (instancetype)initWithFrame:(CGRect)frame andImageNameArray:(NSArray *)ImgNameArray{
    self = [super initWithFrame:frame];
    if (self) {
        self.timerInterval = 2.0f;
        self.carouselArray = ImgNameArray;
        self.pageCount = ImgNameArray.count;
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI{
    self.carouselScroll = [[UIScrollView alloc] init];
    self.carouselScroll.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    [self addSubview:self.carouselScroll];
    
    self.carouselScroll.contentSize = CGSizeMake((self.carouselArray.count + 1)*self.frame.size.width, 0);
    self.carouselScroll.pagingEnabled = YES;
    self.carouselScroll.showsHorizontalScrollIndicator = NO;
    self.carouselScroll.delegate = self;
    
    for (int i = 0; i < self.carouselArray.count + 1; i ++) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.frame.size.width, 0, self.frame.size.width, self.frame.size.width)];
        if (i == self.carouselArray.count) {
            imgView.image = [UIImage imageNamed:self.carouselArray[0]];
            
        }else{
            imgView.image = [UIImage imageNamed:self.carouselArray[i]];
        }
        [self.carouselScroll addSubview:imgView];
    }
    
    self.carouselPageControl = [[UIPageControl alloc] init];
    self.carouselPageControl.frame = CGRectMake(0, self.frame.size.height - 25, self.frame.size.width, 25);
    self.carouselPageControl.numberOfPages = self.pageCount;
    self.carouselPageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    [self addSubview:self.carouselPageControl];
    
    //创建定时器
    [self creatTime];
}

// 创建定时器
- (void)creatTime{
    self.carouselTime = [NSTimer scheduledTimerWithTimeInterval:self.timerInterval target:self selector:@selector(changeScrollviewOffset) userInfo:nil repeats:YES];
    
    //调整定时器的优先级
    NSRunLoop  *mainLoop = [NSRunLoop mainRunLoop];
    [mainLoop addTimer:self.carouselTime forMode:NSRunLoopCommonModes];
    
    
}

- (void)changeScrollviewOffset{
    
    [self.carouselScroll setContentOffset:CGPointMake((self.carouselPageControl.currentPage + 1) *self.frame.size.width, 0) animated:YES];
    
}

#pragma mark - UISCrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGPoint point = scrollView.contentOffset;
    BOOL isRight = self.oldScrollOffset < point.x;
    self.oldScrollOffset = point.x;
    
    //调整pageControl的当前位置
    if (point.x > self.frame.size.width * (self.pageCount - 1) + self.frame.size.width * 0.5 && !self.carouselTime) {
        self.carouselPageControl.currentPage = 0;
    }else if (point.x > self.frame.size.width * (self.pageCount -1) && self.carouselTime && isRight){
        self.carouselPageControl.currentPage = 0;
    }else {
        self.carouselPageControl.currentPage = (point.x + self.frame.size.width *0.5)/self.frame.size.width;
    }
    
    
    //处理情况
    if(point.x >= self.frame.size.width * self.pageCount){ //偏移量超出scrollview contentsize最大值时
        [scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
        
    }else if (point.x < 0){ //偏移量小于0时
        [scrollView setContentOffset:CGPointMake(point.x + self.frame.size.width*(self.pageCount), 0) animated:NO];
    }
    
}

 // 手指开始拖动的时候,就让计时器停止
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    [self stopTimer];
}

- (void)stopTimer{
    [self.carouselTime invalidate];
    self.carouselTime = nil;
}


//手指离开屏幕的时候,就让定时器开始工作
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    [self creatTime];
}

@end
